{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/Users/tazarov/experiments/chroma-experiments/authz-tenant-db-hook\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'ids': ['1'],\n",
       " 'embeddings': None,\n",
       " 'metadatas': [None],\n",
       " 'documents': ['test21']}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%cd ../../../\n",
    "import chromadb\n",
    "from chromadb.config import Settings\n",
    "\n",
    "client = chromadb.HttpClient(\"http://localhost:8000/\",\n",
    "                             settings=Settings(chroma_client_auth_provider=\"chromadb.auth.token.TokenAuthClientProvider\",\n",
    "                                               chroma_client_auth_credentials=\"test-token-admin\"))\n",
    "\n",
    "client.list_collections()\n",
    "collection = client.get_or_create_collection(\"test_collection\")\n",
    "\n",
    "collection.add(documents=[\"test21\"],ids=[\"1\"])\n",
    "collection.get(ids=[\"1\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "ename": "HTTPError",
     "evalue": "400 Client Error: Bad Request for url: http://localhost:8000/api/v1/collections/4487accd-6160-454c-a5f2-26d6e87ce5ef/upsert",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mHTTPError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m/Users/tazarov/experiments/chroma-experiments/chroma-authz/examples/basic_functionality/authz/authz.ipynb Cell 2\u001b[0m line \u001b[0;36m6\n\u001b[1;32m      <a href='vscode-notebook-cell:/Users/tazarov/experiments/chroma-experiments/chroma-authz/examples/basic_functionality/authz/authz.ipynb#W1sZmlsZQ%3D%3D?line=1'>2</a>\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39mchromadb\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mapi\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mmodels\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mCollection\u001b[39;00m \u001b[39mimport\u001b[39;00m Collection\n\u001b[1;32m      <a href='vscode-notebook-cell:/Users/tazarov/experiments/chroma-experiments/chroma-authz/examples/basic_functionality/authz/authz.ipynb#W1sZmlsZQ%3D%3D?line=4'>5</a>\u001b[0m col \u001b[39m=\u001b[39m Collection(client, \u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mtest-upsert-\u001b[39m\u001b[39m{\u001b[39;00muuid\u001b[39m.\u001b[39muuid4()\u001b[39m}\u001b[39;00m\u001b[39m\"\u001b[39m, uuid\u001b[39m.\u001b[39muuid4())\n\u001b[0;32m----> <a href='vscode-notebook-cell:/Users/tazarov/experiments/chroma-experiments/chroma-authz/examples/basic_functionality/authz/authz.ipynb#W1sZmlsZQ%3D%3D?line=5'>6</a>\u001b[0m col\u001b[39m.\u001b[39;49mupsert(documents\u001b[39m=\u001b[39;49m[\u001b[39m\"\u001b[39;49m\u001b[39mtest\u001b[39;49m\u001b[39m\"\u001b[39;49m],ids\u001b[39m=\u001b[39;49m[\u001b[39m\"\u001b[39;49m\u001b[39m1\u001b[39;49m\u001b[39m\"\u001b[39;49m])\n",
      "File \u001b[0;32m~/experiments/chroma-experiments/chroma-authz/chromadb/api/models/Collection.py:299\u001b[0m, in \u001b[0;36mCollection.upsert\u001b[0;34m(self, ids, embeddings, metadatas, documents)\u001b[0m\n\u001b[1;32m    283\u001b[0m \u001b[39m\u001b[39m\u001b[39m\"\"\"Update the embeddings, metadatas or documents for provided ids, or create them if they don't exist.\u001b[39;00m\n\u001b[1;32m    284\u001b[0m \n\u001b[1;32m    285\u001b[0m \u001b[39mArgs:\u001b[39;00m\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    292\u001b[0m \u001b[39m    None\u001b[39;00m\n\u001b[1;32m    293\u001b[0m \u001b[39m\"\"\"\u001b[39;00m\n\u001b[1;32m    295\u001b[0m ids, embeddings, metadatas, documents \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_validate_embedding_set(\n\u001b[1;32m    296\u001b[0m     ids, embeddings, metadatas, documents\n\u001b[1;32m    297\u001b[0m )\n\u001b[0;32m--> 299\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_client\u001b[39m.\u001b[39;49m_upsert(\n\u001b[1;32m    300\u001b[0m     collection_id\u001b[39m=\u001b[39;49m\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mid,\n\u001b[1;32m    301\u001b[0m     ids\u001b[39m=\u001b[39;49mids,\n\u001b[1;32m    302\u001b[0m     embeddings\u001b[39m=\u001b[39;49membeddings,\n\u001b[1;32m    303\u001b[0m     metadatas\u001b[39m=\u001b[39;49mmetadatas,\n\u001b[1;32m    304\u001b[0m     documents\u001b[39m=\u001b[39;49mdocuments,\n\u001b[1;32m    305\u001b[0m )\n",
      "File \u001b[0;32m~/experiments/chroma-experiments/chroma-authz/chromadb/api/fastapi.py:382\u001b[0m, in \u001b[0;36m_upsert\u001b[0;34m(self, collection_id, ids, embeddings, metadatas, documents)\u001b[0m\n\u001b[1;32m    379\u001b[0m batch \u001b[39m=\u001b[39m (ids, embeddings, metadatas, documents)\n\u001b[1;32m    380\u001b[0m validate_batch(batch, {\u001b[39m\"\u001b[39m\u001b[39mmax_batch_size\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mmax_batch_size})\n\u001b[1;32m    381\u001b[0m resp \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_submit_batch(\n\u001b[0;32m--> 382\u001b[0m     batch, \u001b[39m\"\u001b[39m\u001b[39m/collections/\u001b[39m\u001b[39m\"\u001b[39m \u001b[39m+\u001b[39m \u001b[39mstr\u001b[39m(collection_id) \u001b[39m+\u001b[39m \u001b[39m\"\u001b[39m\u001b[39m/update\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m    383\u001b[0m )\n\u001b[1;32m    384\u001b[0m resp\u001b[39m.\u001b[39mraise_for_status()\n\u001b[1;32m    385\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mTrue\u001b[39;00m\n",
      "File \u001b[0;32m~/experiments/chroma-experiments/chroma-authz/venv/lib/python3.11/site-packages/requests/models.py:1021\u001b[0m, in \u001b[0;36mResponse.raise_for_status\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m   1016\u001b[0m     http_error_msg \u001b[39m=\u001b[39m (\n\u001b[1;32m   1017\u001b[0m         \u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39m{\u001b[39;00m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mstatus_code\u001b[39m}\u001b[39;00m\u001b[39m Server Error: \u001b[39m\u001b[39m{\u001b[39;00mreason\u001b[39m}\u001b[39;00m\u001b[39m for url: \u001b[39m\u001b[39m{\u001b[39;00m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39murl\u001b[39m}\u001b[39;00m\u001b[39m\"\u001b[39m\n\u001b[1;32m   1018\u001b[0m     )\n\u001b[1;32m   1020\u001b[0m \u001b[39mif\u001b[39;00m http_error_msg:\n\u001b[0;32m-> 1021\u001b[0m     \u001b[39mraise\u001b[39;00m HTTPError(http_error_msg, response\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m)\n",
      "\u001b[0;31mHTTPError\u001b[0m: 400 Client Error: Bad Request for url: http://localhost:8000/api/v1/collections/4487accd-6160-454c-a5f2-26d6e87ce5ef/upsert"
     ]
    }
   ],
   "source": [
    "import uuid\n",
    "from chromadb.api.models.Collection import Collection\n",
    "\n",
    "col = Collection(client, f\"test-upsert-{uuid.uuid4()}\", uuid.uuid4())\n",
    "col.upsert(documents=[\"test\"],ids=[\"1\"])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
